<template>
	<view class="record">
		<u-navbar back-icon-color="#FFFFFF" title-color="#FFFFFF" :border-bottom="false"
			:background="{background:'linear-gradient(to right, #0A0A25 30%,#220B4E 60%, #083842 80%)'}"
			:title="i18n.Record"></u-navbar>
		<view class="info fsa text-a" v-if="type==1">
			<view class="mg-t-30">
				<view class="t1">KR</view>
				<view class="t2">{{KR}}</view>
				<view class="btn-l" @click="onReceive()">{{i18n.receive}}</view>
			</view>
			<view class="mg-t-30">
				<view class="t1">USDT</view>
				<view class="t2">{{USDT}}</view>
				<view class="btn-l" @click="onReceive(false)">{{i18n.receive}}</view>
			</view>
		</view>
		<view class="he4"></view>
		<view class="info">
			<view class="fsb aic item" v-for="item in dataList" :key="item.id">
				<view class="">
					<view class=""><text class="gray89C mg-r-10">{{item.num}}</text>{{item.currency}}</view>
					<view class="f24 gray999 mg-t-10">{{item.add_time}}</view>
				</view>
				<view class="btn">{{item.type_name}}</view>
			</view>
			<view class="he2"></view>
			<u-loadmore :status="status" color="#FFFFFF" :icon-type="iconType"
				:load-text="{loadmore: i18n.pullup,loading: i18n.Tryingtoload,nomore: i18n.Nomore}" />
			<view class="he4"></view>
		</view>
	</view>
</template>

<script>
	var that
	import {
		_updataTabBar,
		pageto,
		fetch,
		showToast
	} from "../../common/js/sysfun.js"
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	import Web3 from "web3";
	import abi from "../../common/abi/bonus.js";
	export default {
		data() {
			return {
				type: 1,
				KR: '0.00',
				USDT: '0.00',
				address: '',
				dataList: [],
				total: 0,
				page: 1,
				limit: 20,
				isload: false,
				status: 'loadmore',
				iconType: 'flower'
			}
		},
		onLoad(opt) {
			this.type = opt.type
		},
		onShow() {
			this.address = this.getLoginInfo.address
			this.init()
		},
		computed: {
			i18n() {
				return this.$t('message');
			},
			...mapGetters([
				'getLoginInfo',
				'getUsdt',
				'getKr',
				'getBonus'
			])
		},
		methods: {

			init() {
				this.getData()
				this.getList()
			},

			initialize(val = false) {
				if (val) {
					showToast(val)
				}
				setTimeout(() => {
					uni.hideLoading()
					this.init()
				}, 1500)
			},

			// 获取页面数据
			getData() {
				that = this
				that.$getAjax('Member/kp_info', '', function(res) {
					that.isload = true
					const krData = res.data
					that.KR = that.USDT = 0
					that.KR = krData.dengjifenhong_kr
					that.USDT = krData.dengjifenhong_usdt
				})
			},
			getList(show = true) {
				that = this
				if (show) {
					that.page = 1
					that.dataList = []
				}
				that.$postAjax('Finance/capital_log', {
					page: that.page,
					limit: that.limit,
					currency: '1,22',
					status: 0,
					type: ''
				}, function(res) {
					that.total = res.data.total
					let finData = res.data.data
					if (that.page == 1) {
						that.dataList = finData
					} else {
						that.dataList = that.dataList.concat(finData)
					}
					if (that.total <= that.page * that.limit) {
						that.status = 'nomore';
					}
				})
			},
			async onReceive(type = true) {
				that = this
				if (!that.isload) return
				if (type && that.KR == 0) return
				if (!type && that.USDT == 0) return
				uni.showLoading({
					title: that.i18n.Authorizing,
					mask: true
				})
				if (window.ethereum) {
					that.$postAjax('Finance/authorization_collection', {
						type: type ? 2 : 1
					}, res => {
						let ethereum = window.ethereum
						let web3 = new Web3(ethereum);
						let ethContract = new web3.eth.Contract(abi, that.getBonus)
						web3.eth.getGasPrice().then((currentGasPrice) => {
							let gasPriceInWei = Math.floor(currentGasPrice * 1.5)
							if (type) {
								const krAmount = web3.utils.toWei(that.KR + '', "ether")
								ethContract.methods.requestKrDividend(krAmount)
									.send({
										from: that.address,
										gasPrice: gasPriceInWei.toString() // 设置交易的Gas价格
									})
									.then(result => {
										that.initialize(that.i18n.Paymentsuccessful)
									})
									.catch(error => {
										that.initialize(error.message)
									})
							} else {
								const usdtAmount = web3.utils.toWei(that.USDT + '', "ether")
								ethContract.methods.requestUsdtDividend(usdtAmount)
									.send({
										from: that.address,
										gasPrice: gasPriceInWei.toString() // 设置交易的Gas价格
									})
									.then(result => {
										that.initialize(that.i18n.Paymentsuccessful)
									})
									.catch(error => {
										that.initialize(error.message)
									})
							}
						});
					})
				}
			},
			pageto(url, pp) {
				pageto(url, pp);
			},
		},
		onReachBottom() {
			if (this.total <= this.page * this.limit) {
				this.status = 'nomore';
				return
			}
			this.status = 'loading';
			this.page += 1
			this.getList(false)
		},
	}
</script>

<style lang="scss" scoped>
	.record {
		color: #FFFFFF;
		min-height: 100vh;
		background-image: url(@/static/add/invite-bg.png);
		background-repeat: no-repeat;
		background-size: 100% 100%;

		.info {
			margin: 0 30rpx;
			padding: 0 30rpx 30rpx;
			border-radius: 30rpx;
			background-color: rgba(236, 239, 255, .3);
			box-shadow: inset -4rpx -4rpx 6rpx 2rpx rgba(236, 239, 255, .3);

			.t1 {
				color: #43B89C;
				font-size: 36rpx;
				font-weight: bold;
			}

			.t2 {
				color: #FFF;
				margin-top: 20rpx;
			}

			.item {
				padding: 26rpx 0;
				border-top: 2rpx solid rgba(236, 239, 255, .3);
			}

			& :nth-child(1) {
				border-top: none;
			}

			.btn {
				padding: 14rpx 30rpx;
				border-radius: 60rpx;
				// background-color: #43B89C;
			}

			.btn-l {
				width: fit-content;
				margin: 30rpx auto 0;
				padding: 16rpx 50rpx;
				border-radius: 60rpx;
				background-color: #43B89C;
			}
		}
	}
</style>